package br.com.webhomesites.locacao.presenter;

import br.com.webhomesites.locacao.contracts.ILocacaoPresenter;
import br.com.webhomesites.locacao.contracts.ILocacaoView;
import br.com.webhomesites.locacao.model.LocacaoBO;
import br.com.webhomesites.locacao.view.LocacaoView;
import br.com.webhomesites.padrao.model.ProdutoDAO;
import br.com.webhomesites.padrao.utils.Mascarador;
import java.awt.Component;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

public class LocacaoPresenterActionListeners {

    static class AlterarActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public AlterarActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.UPDATE_STRATEGY);
            presenter.habilitarEdicao(true);

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

            view.enableBtnInserirItem(true);
            view.enableBtnRemoverItem(true);
            view.enableBtnCancelarLocacao(true);
            view.enableBtnSalvarLocacao(true);
            //view.enableBtnAlterar(true); 

        }
    }

    static class CancelarLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public CancelarLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();
             
            view.clearFieldsPainelLocacao();
            view.setComboClientesFocus();
            
            view.enableBtnNovaLocacao(true);
            view.enableBtnCancelarLocacao(false);
            view.enableBtnSalvarLocacao(false);


        }
    }

    static class CancelarItemLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public CancelarItemLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

            view.clearFields();

            //presenter.habilitarEdicao(false);  
            view.clearFieldsPainelIncluirProdutos();
            view.setComboProdutosFocus();
            
            view.enableBtnInserirItem(true);
            view.enableBtnRemoverItem(false);
            view.enableBtnCancelarItem(false);
            view.enableBtnSalvarItem(false);

        }
    }

    static class SalvarLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public SalvarLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.getOperacao().execute();
            //presenter.setOperacao(presenter.UPDATE_STRATEGY);
            //presenter.habilitarEdicao(false);  

            //setUpViewListeners();

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

            view.enableBtnNovaLocacao(true);
            view.enableBtnCancelarLocacao(false);
            view.enableBtnSalvarLocacao(false);
            view.enableBtnSalvarItem(false);
            view.enableBtnCancelarItem(false);
            view.enableBtnRemoverItem(false);
            view.enableBtnInserirItem(true);

            System.out.println("Passou na ação salvar!");

        }
    }

    static class SalvarItemLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public SalvarItemLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.getOperacao().execute();
            presenter.setOperacao(presenter.INSERT_ITEM_STRATEGY);
            //presenter.habilitarEdicao(false);  

            //setUpViewListeners();

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

            view.enableBtnInserirItem(true);
            view.enableBtnSalvarItem(false);
            view.enableBtnCancelarItem(false);
            view.enableBtnRemoverItem(false);

            System.out.println("Passou na ação salvar!");

        }
    }

    static class InserirLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;
        private static int codigoLocacao = 0;

        public InserirLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.INSERT_STRATEGY);
            presenter.novoLocacao();

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();
            
            presenter.habilitarEdicao(true);
            view.clearFieldsPainelLocacao();
            view.clearFieldsPainelIncluirProdutos();

            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

            view.setDataLocacao(formatter.format(date));

            view.enableBtnNovaLocacao(false);
            view.enableBtnSalvarLocacao(true);
            view.enableBtnCancelarLocacao(true);
            
            view.enableBtnInserirItem(false);
            view.enableBtnSalvarItem(false);
            view.enableBtnRemoverItem(false);
            view.enableBtnCancelarItem(false);
            
            codigoLocacao = codigoLocacao + 1;
            view.setCodigoLocacao(codigoLocacao);
            view.clearTableItensLocacao();
        }
    }

    static class InserirItemLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public InserirItemLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.INSERT_ITEM_STRATEGY);
            presenter.novoItemLocacao();

            ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

            presenter.habilitarEdicao(true);
            view.clearFields();

            view.enableBtnInserirItem(false);
            view.enableBtnSalvarItem(true);
            view.enableBtnCancelarItem(true);
            view.enableBtnRemoverItem(false);

        }
    }

    static class RemoverItemLocacaoActionListener implements ActionListener {

        private LocacaoPresenter presenter;

        public RemoverItemLocacaoActionListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            if (JOptionPane.showConfirmDialog((Component) presenter.getLocacaoView(), "Deseja remover?", "Pergunta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {

                ILocacaoView view = (ILocacaoView) presenter.getLocacaoView();

                presenter.setOperacao(presenter.REMOVE_ITEM_STRATEGY);
                presenter.getOperacao().execute();
                view.clearFields();

                view.enableBtnInserirItem(true);
                view.enableBtnSalvarItem(false);
                view.enableBtnCancelarItem(false);
                view.enableBtnRemoverItem(false);

            }
        }
    }

    static class SairActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.exit(0);
        }
    }

    static class CampoKeyListener implements KeyListener {

        private LocacaoPresenter presenter;
        ILocacaoView view;
        Mascarador mask;

        public CampoKeyListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (ILocacaoView) presenter.getLocacaoView();
            mask = new Mascarador();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent e) {

            view.setDataLocacao(mask.getMascara(view.getDataLocacao(), mask.RG, e));
            view.setDataPrevDevolucao(mask.getMascara(view.getDataPrevDevolucao(), mask.CNPJ, e));
        }
    }

    static class ComboStateListener implements ItemListener {

        private LocacaoPresenter presenter;
        ILocacaoView view;

        public ComboStateListener(LocacaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (ILocacaoView) presenter.getLocacaoView();

        }

        @Override
        public void itemStateChanged(ItemEvent ie) {

            System.out.println("Mudou de estado");
            ProdutoDAO produtoDAO = new ProdutoDAO();
            presenter.updateViewItemFromProdutoModel(produtoDAO.selecionarProduto(view.getDescricaoItemLocacao()));
        }
    }

    static class EnterListener implements ActionListener {

        private LocacaoPresenter locacaoPresenter;

        public EnterListener(LocacaoPresenter locacaoPresenter) {
            this.locacaoPresenter = locacaoPresenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            LocacaoBO locacaoBO = new LocacaoBO(locacaoPresenter);
            locacaoBO.setCalcDataPrevDevolucao();
            System.out.println("Passou na ação");
        }
    }
    
    static class TableClientesMouseListener extends MouseAdapter {  
        private LocacaoPresenter locacaoPresenter;  
          
        public TableClientesMouseListener(LocacaoPresenter locacaoPresenter) {  
            this.locacaoPresenter = locacaoPresenter;  
        }  
          
        @Override
        public void mouseClicked(MouseEvent e) {  
              
            locacaoPresenter.updateModelFromJTable();              
               
            //LocacaoPresenter.habilitarEdicao(false);
              
            ILocacaoView locacaoView = (LocacaoView)locacaoPresenter.getLocacaoView();  
              
            locacaoView.enableBtnCancelarItem(false);  
            locacaoView.enableBtnSalvarItem(false);  
            locacaoView.enableBtnInserirItem(true);
            locacaoView.enableBtnRemoverItem(true);         
        
        }  
    }
}
